package com.muqi.app.pj.shops;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.application.ActivitiesManager;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.LogUtil;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.MenuActivity;
import com.muqi.app.qlearn.student.R;
import com.wenhui.pedant.SweetAlert.SweetAlertDialog;
import com.wenhui.pedant.SweetAlert.SweetAlertDialog.OnSweetClickListener;

public class OrderIdActivity extends BaseFragmentActivity implements
		OnClickListener {
	public static final String ORDER_NUMBER = "";
	private TextView mOrderId;
	private TextView mPayMoney;

	private Button mRealPay;

	private String mOrderIdStr;
	private String mPayMoneyStr;

	private int type = 0; /* 0 默认的代表 积分商城中的 实物商品 1 代表 虚拟商品的兑换 */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty_order_id);
	}

	@Override
	protected void onDestroy() {
		ActivitiesManager.getInstance().pop(this);
		super.onDestroy();
	}

	@Override
	protected void onInit() {
		ActivitiesManager.getInstance().push(this);
		mOrderId = (TextView) findViewById(R.id.course_order_id);
		mPayMoney = (TextView) findViewById(R.id.buy_course_money);
		mRealPay = (Button) findViewById(R.id.real_pay);

		mRealPay.setOnClickListener(this);
	}

	@Override
	protected void onData() {
		Intent i = getIntent();
		mOrderIdStr = i.getStringExtra("orderId");
		mPayMoneyStr = i.getStringExtra("payMoney");
		type = i.getIntExtra("type", 0);
		mOrderId.setText(mOrderIdStr);
		mPayMoney.setText(mPayMoneyStr);

	}

	public void onBack(View v) {
		this.finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.real_pay:
			payWithGoodsOrder(mOrderIdStr, "");
			break;
		default:
			break;
		}
	}

	private void payWithGoodsOrder(String orderNo, String password) {
	
			if (NetWorkUtils.isNetworkAvailable(this)) {
				//Log.e("===", orderNo);
				HashMap<String, Object> sendMap = new HashMap<String, Object>();
				sendMap.put("token", mSpUtil.getToken());
				sendMap.put("dingdan_id", orderNo);
				sendMap.put("jine", mPayMoneyStr);
				sendMap.put("zhifutixianpass", password);
				String requestUrl = ParamsUtils.buildParams(
						NetWorkApi.PAY_WITH_SCORE_GOODS_API, sendMap);
				MyAsyncHttp.get(this, requestUrl, new AsyncCallBackListener() {

					@Override
					public void onAsyncCallBack(RESULTCODE resuleCode,
							String request, String result) {
						if (resuleCode == RESULTCODE.SUCCESS) {
							showToast("兑换成功");
							startActivity(MenuActivity.class);
							//setResult(RESULT_OK);
							finish();
						}else{
							showToast(result);
						}
					}
				});
			} else {
				ShowToast.showShort(this, R.string.net_break);// 提示无网络
			}

	}

	protected void payOrder(final SweetAlertDialog mDialog) {
		showLoading();

	}

}
